package com.smartwebx.fr.test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.MapMaker;
import com.google.common.collect.Maps;

public class GoogMap {

	@Test
	public void test22() throws InterruptedException, ExecutionException, TimeoutException {
		ExecutorService service = Executors.newFixedThreadPool(5);
		List<TestData> buildReals = Lists.newArrayList();
		for (int i=0;i<10;i++) {
			buildReals.add(new TestData(i));
		}
		Integer futrue = service.invokeAny(buildReals, 10, TimeUnit.SECONDS);
		
		service.shutdown();
		System.out.println(futrue);
		
	}

	class TestData implements Callable<Integer> {
		private int i;

		public TestData(int i) {
			this.i = i;
		}

		@Override
		public Integer call() throws Exception {
			return i;
		}
	}

	@Test
	public void testStr() {
		String str = "select Z from jssq.st_river_r where stcd='60115000' and tm=to_date('#{日期} 08:00:00','yyyy-mm-dd hh24:mi:ss')";
		Map<String, String> nameValMap = Maps.newHashMap();
		nameValMap.put("日期", "2015-10-16");

		String[] findSigns = { "\"", "'" };
		List<Integer[]> startEnds = Lists.newArrayList();
		int start = -1;
		int end = -1;
		for (String sign : findSigns) {
			while (true) {
				start = str.indexOf(sign, start + 1);
				if (start == -1)
					break;
				end = start;
				end = str.indexOf(sign, end + 1);
				startEnds.add(new Integer[] { start, end });
				start = end;
			}
		}

		List<Integer[]> replaceStartEnds = Lists.newArrayList();
		List<String> sqlParams = Lists.newArrayList();
		for (Integer[] startEnd : startEnds) {
			String subStr = str.substring(startEnd[0] + 1, startEnd[1]);
			boolean isFind = false;
			for (String key : nameValMap.keySet()) {
				if (subStr.indexOf(key) >= 0) {
					subStr = StringUtils.replace(subStr, "#{" + key + "}",
							nameValMap.get(key));
					isFind = true;
				}
			}
			if (isFind) {
				sqlParams.add(subStr);
				replaceStartEnds.add(startEnd);
			}
		}

		if (!replaceStartEnds.isEmpty()) {
			StringBuffer strBuffer = new StringBuffer(str);
			for (int i = replaceStartEnds.size() - 1; i >= 0; i--) {
				strBuffer = strBuffer.replace(replaceStartEnds.get(i)[0],
						replaceStartEnds.get(i)[1] + 1, "?");
			}
			str = strBuffer.toString();
		}

		System.out.println(str);
	}

	@Test
	public void testSubSql() {
		String target = "啦啦啦'#{abcd}',2222#{bdef}555,4444#{abcd}";
		Map<String, String> nameValMap = Maps.newHashMap();
		nameValMap.put("abcd", "1234");
		nameValMap.put("bdef", "457");

		List<String> subSqlParams = Lists.newArrayList();
		int findIndex = -1;
		int endIndex = -1;
		while (true) {
			findIndex = target.indexOf("#{", findIndex + 1);
			endIndex = target.indexOf("}", endIndex + 1);
			if (findIndex == -1)
				break;
			String paramKey = target.substring(findIndex + 2, endIndex);
			String paramVal = nameValMap.get(paramKey);
			subSqlParams.add(paramVal);
		}

		Collection<String> searchKey = nameValMap.keySet();

		// 替换字符不能用 '' 包裹
		String[] replaceSigns = { "\'", "" };
		for (String sign : replaceSigns) {
			String[] searchKeyArr = new String[searchKey.size()];
			String[] searchValArr = new String[searchKey.size()];
			for (int i = 0; i < searchKey.size(); i++) {
				searchKeyArr[i] = sign + "#{" + Iterables.get(searchKey, i)
						+ "}" + sign;
				searchValArr[i] = "?";
			}
			target = StringUtils.replaceEachRepeatedly(target, searchKeyArr,
					searchValArr);
		}

		System.out.println(target);
	}

	@Test
	public void testMap() {
		HashMultimap<String, String> map = HashMultimap.create();
		for (Integer i = 0; i < 10; i++) {
			map.put("a_" + i, "b_" + (i + 1000));
		}
		Map<String, Collection<String>> map1 = map.asMap();
		for (String key : map1.keySet()) {
			map1.get(key);
		}

	}

	@Test
	public void testMap1() {
		// final String aa = "aaaaaa";
		//
		// ConcurrentMap<String, Object> map5 = new MapMaker()
		// .makeComputingMap(new Function<String, Object>() {
		// public Object apply(String key) {
		// return createObject(key);
		// }
		//
		// private Object createObject(String key) {
		//
		// return aa;
		// }
		// });
		//
		// map5.get("aaa");
		//
		// System.out.println(map5);

	}

	/**
	 * 效率不如原生态的
	 */
	@Test
	public void testMap2() {
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			ConcurrentMap<Object, Object> map = new ConcurrentHashMap<Object, Object>();
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);

		startTime = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			ConcurrentMap<Object, Object> a = new MapMaker().makeMap();
		}
		endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);

	}

	public static void main(String[] args) throws Exception {
		createImage("2.056", new Font("微软雅黑", Font.PLAIN, 18), new File(
				"d:/a.png"));
		createImage("中华人民", new Font("微软雅黑", Font.PLAIN, 18), new File(
				"d:/a1.png"));
		createImage("中华人民共和国", new Font("黑体", Font.PLAIN, 24), new File(
				"d:/a2.png"));
	}

	// 根据str,font的样式以及输出文件目录
	public static void createImage(String str, Font font, File outFile)
			throws Exception {
		// 获取font的样式应用在str上的整个矩形
		Rectangle2D r = font.getStringBounds(str, new FontRenderContext(
				AffineTransform.getScaleInstance(1, 1), false, false));
		int unitHeight = (int) Math.floor(r.getHeight());// 获取单个字符的高度
		// 获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
		int width = (int) Math.round(r.getWidth()) + 1;
		int height = unitHeight + 3;// 把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
		// 创建图片
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_BGR);
		Graphics g = image.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);// 先用白色填充整张图片,也就是背景
		g.setColor(Color.black);// 在换成黑色
		g.setFont(font);// 设置画笔字体
		g.drawString(str, 0, font.getSize());// 画出字符串
		g.dispose();
		ImageIO.write(image, "png", outFile);// 输出png图片
	}
}
